/*
ADS8320
*/

#include "p24HJ256GP206.h"
#include "ADS8320.h"
#include "main.h"		// Per recupero valore frequenza 


long Read_ADS8320(void)
	{
	unsigned char count = 0;
	unsigned int out_ADC =0;
	unsigned char dout[16];

	//TRISGbits.TRISG7=1; // DOUT
	TRISFbits.TRISF2=1; // DOUT

	//TRISGbits.TRISG6=0; // DSCLK
	TRISFbits.TRISF6=0; // DSCLK

	TRISDbits.TRISD2=0; // CS

	DSCLK=1;
	CS_ADC1=0;
	
	for (count=0;count<6;count++)
		{
		DSCLK=0;
		Delay_SCK();
		DSCLK=1;
		Delay_SCK();
		}

	for (count=0;count<16;count++)
		{
		DSCLK=0;
		Delay_SCK();
		DSCLK=1;
		Delay_SCK();
		if(DOUT)
			out_ADC = out_ADC|(0x8000>>count);
			dout[count]=DOUT;
		}

	DSCLK=1;
	CS_ADC1=1;
	return (long)out_ADC;
	} 

void Delay_SCK(void)
	{
	float temp;
	long int cycles=1;
	temp=(float)(cycles*(FREQ/8000000));
	cycles=(long int)temp;
	while(cycles>0)
	{cycles--;}
	return;
	}

